package com.heima.article.service.impl;

import com.heima.article.service.ApArticleBehaviorDetailService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.vos.ArticleInfoVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ApArticleBehaviorDetailServiceImpl implements ApArticleBehaviorDetailService {


    private final CacheService cacheService;
    /**
     * 返回用户对文章做了什么行为的详细记录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehaviour(ArticleInfoDto dto) {
        // 1、用户检查
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String userId = user.getId().toString();
        boolean islike=selectLikeFromRedisAndDb(dto.getArticleId(), userId);
        boolean isunlike = selectDislikeFromRedisAndDb(dto.getArticleId(), userId);
        boolean iscollect = selectCollectionFromRedisAndDb(dto.getArticleId(), userId);
        // String followKey = BehaviorConstants.FOLLOW_AUTHOR + "_" + dto.getAuthorId();
        boolean isfollow = selectFollowFromRedisAndDb(dto.getAuthorId(), userId);
        // TODO：如果redis没查到，查Mysql
        ArticleInfoVo articleInfoVo = new ArticleInfoVo(islike, iscollect, isfollow, isunlike);
        return ResponseResult.okResult(articleInfoVo);
    }

    /**
     * 查询文章点赞状态
     * @param articleId 文章id
     * @param userId 用户id
     * @return
     */
    private boolean selectLikeFromRedisAndDb(Long articleId, String userId) {
        // 1、从redis中查询
        String likeKey = BehaviorConstants.ARTICLE_LIKE + "_" + articleId;
        Object value=cacheService.hGet(likeKey, userId);
        if(value!=null){
            Short value1 = Short.valueOf((String) value);
            if(value1.equals(BehaviorConstants.ARTICLE_LIKE_ACTION)){
                return true;
            }
            return false;
        }
        // TODO:redis没有 从Db查
        return false;
    }
    /**
     * 查询文章不喜欢状态
     * @param articleId 文章id
     * @param userId 用户id
     * @return
     */
    private boolean selectDislikeFromRedisAndDb(Long articleId, String userId) {
        // 1、从redis中查询
        String dislikeKey = BehaviorConstants.ARTICLE_DISLIKE + "_" + articleId;
        Object value=cacheService.hGet(dislikeKey, userId);
        if(value!=null){
            Short value1 = Short.valueOf((String) value);
            if(value1.equals(BehaviorConstants.ARTICLE_DISLIKE_ACTION)){
                return true;
            }
            return false;
        }
        // TODO:redis没有 从Db查
        return false;
    }
    /**
     * 查询文章收藏状态
     * @param articleId 文章id
     * @param userId 用户id
     * @return
     */
    private boolean selectCollectionFromRedisAndDb(Long articleId, String userId) {
        // 1、从redis中查询
        String collectKey = BehaviorConstants.COLLECT_ARTICLE + "_" + articleId;
        Object value=cacheService.hGet(collectKey, userId);
        if(value!=null){
            Short value1 = Short.valueOf((String) value);
            if(value1.equals(BehaviorConstants.ARTICLE_COLLECT_ACTION)){
                return true;
            }
            return false;
        }
        // TODO:redis没有 从Db查
        return false;
    }
    /**
     * 查询与作者关注状态
     * @param authorId 作者id
     * @param userId 用户id
     * @return
     */
    private boolean selectFollowFromRedisAndDb(Integer authorId, String userId) {
        // 1、从redis中查询
        String followKey = BehaviorConstants.FOLLOW_AUTHOR + "_" + authorId;
        Object value=cacheService.hGet(followKey, userId);
        if(value!=null){
            Short value1 = Short.valueOf((String) value);
            if(value1.equals(BehaviorConstants.ARTICLE_FOLLOW_ACTION)){
                return true;
            }
            return false;
        }
        // TODO:redis没有 从Db查
        return false;
    }


}
